<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 资料收集
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!--<el-button type="success" @click="addEdit">批量下载</el-button>
                <el-button
                    type="primary"
                    icon="el-icon-download"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >打包下载</el-button>-->
                                查询条件: 
                <el-input v-model="query.theme" placeholder="主题" class="handle-input mr10"></el-input>
                <el-input v-model="query.content" placeholder="内容" class="handle-input mr10"></el-input>
                <el-input v-model="query.fileName" placeholder="文件名" class="handle-input mr10"></el-input>
                <el-input v-model="query.realName" placeholder="上传者姓名" class="handle-input mr10"></el-input>
                <el-input v-model="query.orgName" placeholder="上传者班级" class="handle-input mr10"></el-input>
               <!-- <el-date-picker v-model="query.endTime" type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期" class="handle-input mr10"></el-date-picker>-->
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="" icon="el-icon-brush" @click="clearSearch">清空</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="fileSerno" label="文件流水" width="55" align="center" :hidden="true"></el-table-column>
  				<el-table-column prop="tempId" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="theme" label="主题"></el-table-column>
                <el-table-column prop="content" label="内容" align="center"></el-table-column>
                <el-table-column prop="fileName" label="资料模板文件"></el-table-column>
                <el-table-column prop="realName" label="上传者姓名"></el-table-column>
                <el-table-column prop="orgName" label="上传者班级"></el-table-column>
                <el-table-column prop="commitTime" label="提交时间"></el-table-column>
                <!--<el-table-column prop="createTime" label="创建时间"></el-table-column>-->
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            icon="el-icon-download"
                            size="mini"
                            v-has="2"
                            @click="downloadMaterial(scope.$index, scope.row)"
                        >下载资料</el-button>
                        <el-button
                            type=""	
                            size="mini"
                            icon="el-icon-check"
                            v-has="6"
                        >已 提 交 </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="模板详情" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="主题">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input v-model="form.content"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

export default {
    name: 'basetable',
    data() {
        return {
            query: {
                theme: '',
                content: '',
                fileName:'',
                realName:'',
                orgName:'',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            downloadPath:'E:/work/down/1111/',
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        async getData(){
	        let res = await this.$Http.getTemplatedtlList(
	        	{
	        		pageNum:this.query.pageIndex,
	        	},
                true
	        );
	        this.tableData = res.data.list;
	        this.pageTotal = res.data.total || 10;
	    },
        
        //辅导员下载资料
		downloadMaterial(index, row) {
		    let form = new FormData();
	    	var that = this;
	    	console.log('request:tempId='+ row.tempId +',fileName='+row.fileName + ',downloadPath=' + that.downloadPath);
	    	form.append('fileSernos', row.fileSerno);
	    	form.append('downloadPath', that.downloadPath);
	    	that.$axios.post('/template/downloadMaterialByManager.do', form, {
	    		responseType: 'blob',//声明后端返回的数据为二进制流
 
	    	}).then((res) => {
	    		console.log('下载结束');
	    		console.log(res);
	    		console.log(res.data);
	    		let blob = new Blob([res.data], { type: res.headers['content-type']})
				let downloadElement = document.createElement('a')
				let href = URL.createObjectURL(blob)
				//let href = res.data.data.url
				downloadElement.href = href
				//downloadElement.download = /*自定义文件名*/
				document.body.appendChild(downloadElement)
				//downloadElement.click()
				document.body.removeChild(downloadElement)
				window.URL.revokeObjectURL(href)
	    		this.$message.success('下载成功');
	    	}).catch((err) => {
	    		console.log('下载错误:')
	    		console.log(err)
	    	})
		     
		},
//      getData() {
//          fetchData(this.query).then(res => {
//              this.tableData = res.list;
//              this.pageTotal = res.pageTotal || 50;
//          });
//
//      },
        // 触发搜索按钮
        async handleSearch() {
	        let res = await this.$Http.getTemplatedtlListByParams(
	        	{
	        		pageNum:this.query.pageIndex,
	        		theme : this.query.theme,
	        		content:this.query.content,
	        		fileName:this.query.fileName,
	                realName:this.query.realName,
	                orgName:this.query.orgName
	        	},
                true
	        );
	        console.log(res.data);
	        //console.log(res.data.total);
	        this.tableData = res.data.list;
	        this.pageTotal = res.data.total || 10;

        },
        //清空搜索栏
        clearSearch(){
        	this.query.theme=''
	        this.query.content=''
	        this.query.fileName=''
	        this.query.realName=''
	        this.query.orgName=''
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 新增操作
        addEdit(){
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        
        
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
